const dbPool = require("../database");

// 加入购物车路由回调
exports.addToCart = function (req, res) {
  const sqlStr = "select * from goods where id = ?";
  dbPool.query(sqlStr, req.body.goods_id, (err, results) => {
    if (err) return res.cc(err);
    if (results.length !== 1) return res.cc("商品不存在！");
    if (req.body.amount > results[0].stock) return res.cc("商品库存不足！");
    // 查看该用户是否加购过商品
    const sqlStr = "select * from shopping_cart where goods_id = ? and user_id = ?";
    dbPool.query(sqlStr, [req.body.goods_id, req.user.id], (err, results) => {
      if (err) return res.cc(err);
      let sqlStr;
      if (results.length !== 1) {
        // 当该用户没加购过改商品时，插入新的数据
        sqlStr = "insert into shopping_cart set ?";
        dbPool.query(sqlStr, { ...req.body, user_id: req.user.id }, (err, results) => {
          if (err) return res.cc(err);
          if (results.affectedRows !== 1) return res.cc("加入购物车失败！");
          res.cc("加入购物车成功！", 1);
        })
      } else {
        // 当该用户加购过改商品时，修改商品数量
        const amount = results[0].amount + req.body.amount;
        sqlStr = "update shopping_cart set amount = ? where goods_id = ? and user_id = ?";
        dbPool.query(sqlStr, [amount, req.body.goods_id, req.user.id], (err, results) => {
          if (err) return res.cc(err);
          if (results.affectedRows !== 1) return res.cc("加入购物车失败！");
          res.cc("加入购物车成功！", 1);
        })
      }
    })
  })
}

// 获取购物车数据路由回调
exports.getCart = function (req, res) {
  const sqlStr = "select shopping_cart.id, name, price, amount, image, goods_id from goods join shopping_cart on goods.id = shopping_cart.goods_id where user_id = ? order by shopping_cart.id desc";
  dbPool.query(sqlStr, req.user.id, (err, results) => {
    if (err) return res.cc(err);
    res.send(
      {
        status: 1,
        message: "获取购物车信息成功！",
        data: results,
      }
    )
  })
}

// 修改购物车商品数量
exports.upAmount = function (req, res) {
  const sqlStr = "select * from shopping_cart where id = ? and user_id = ?";
  dbPool.query(sqlStr, [req.body.id, req.user.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.length !== 1) return res.cc("修改数量失败！");
    const amount = results[0].amount + req.body.change;
    const goods_id = results[0].goods_id;
    if (amount < 1) return res.cc("数量不能小于1！")
    const sqlStr = "select * from goods where id = ?";
    dbPool.query(sqlStr, goods_id, (err, results) => {
      if (err) return res.cc(err);
      if (amount > results[0].stock) return res.cc("库存不足！")
      const sqlStr = "update shopping_cart set amount = ? where id = ?";
      dbPool.query(sqlStr, [amount, req.body.id], (err, results) => {
        if (err) return res.cc(err);
        if (results.affectedRows !== 1) return res.cc("修改数量失败！");
        res.cc("修改数量成功！", 1);
      })
    })
  });
}

// 删除购物车中的商品
exports.removeCart = function (req, res) {
  const sqlStr = "delete from shopping_cart where id = ? and user_id = ?";
  dbPool.query(sqlStr, [req.body.id, req.user.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("从购物车中删除失败！");
    res.cc("从购物车中删除成功！", 1);
  })
}


// 获取购物车的长度
exports.getLen = function (req, res) {
  const sqlStr = "select count(*) as len from shopping_cart";
  dbPool.query(sqlStr, (err, results) => {
    if (err) res.cc(err);
    res.send({
      status: 1,
      message: "获取购物车数量成功！",
      len: results[0].len,
    })
  })
}